﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessObjects.BO_GetObjects;
using DigiLib;
using System.Security.Cryptography;

public partial class controls_admin_ArticlesAdd : Admin_Controls_Base
{
    protected void Page_Load(object sender, EventArgs e)
    {
        base.removeHGC(divMessage);
        if (!IsPostBack)
        {
            try
            {
                base.ModuleByContent(lsbModule, "ARTICLES", 1);
                base.bindDDL(ddlCategories, BO_portal_categories.InsideContent(0, "ARTICLES"), "FULL_CATEGORY_NAME", "CATEGORY_ID");
            }
            catch (Exception ex)
            {
                divMessage.InnerHtml = MessageBox.Error("Lỗi", "Phát sinh lỗi như sau: " + ex.Message);
            }
        }
    }
    private string random()
    {
        byte[] number = new byte[6];
        RandomNumberGenerator rng = RandomNumberGenerator.Create();
        rng.GetNonZeroBytes(number);
        string random = BitConverter.ToString(number);
        random = random.Replace("-", "");
        return random;
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            string multi = string.Empty;
            string filename = base.Upload("articles", String.Empty, fulTHUMB_IMG, divMessage, false);
            int category_id = Convert.ToInt32(ddlCategories.SelectedValue);
            int status = 1;
            string tags = txtTags.Text.Trim();
            string related = txtRelated.Text.Trim();
            string title = txtFULL_ARTICLE_TITLE.Text.Trim();
            string full_article_description = txtFULL_ARTICLE_DESCRIPTION.Text.Trim();
            string details = ckContent.Text;
            bool displayOnHome = cboDisplay.Checked;
            bool hot = cboHot.Checked;
            int sort_display = Convert.ToInt32(txtSortDisplay.Text.Trim());
            string m_key = txtMetaKeyword.Text.Trim();
            string m_desc = txtMetaDescription.Text.Trim();
            string strResult = BO_portal_articles.Insert(category_id, 1, random(),
                             multi, filename, string.Empty, string.Empty, title, string.Empty, string.Empty
                             , string.Empty, full_article_description, string.Empty,
                             details, string.Empty, m_key, m_desc, tags, 0,
                            0, 0, 0, displayOnHome, hot, sort_display, sort_display, DateTime.Now, DateTime.Now,
                            Administrator.USER_NAME, DateTime.Now, Administrator.USER_NAME, DateTime.Now, Administrator.USER_NAME, status);
            if (strResult != "0")
            {
                divMessage.InnerHtml = MessageBox.Success("Thông Báo", "Đăng tin thành công!");
                foreach (ListItem item in lsbModule.Items)
                {
                    if (item.Selected)
                    {
                        BO_cms.Join_Tables_Insert(Convert.ToInt32(strResult), Convert.ToInt32(item.Value), "FK_portal_articles_portal_modules", sort_display, 0);
                    }
                }
                foreach (string tag in tags.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
                {
                    if (!string.IsNullOrEmpty(tag))
                        try
                        {
                            int id_1 = Convert.ToInt32(BO_portal_tags.Insert(tag.Trim(), 0, DateTime.Now, Administrator.USER_NAME, DateTime.Now, Administrator.USER_NAME, true));
                            if (id_1 > 0) BO_cms.Join_Tables_Insert(id_1, Convert.ToInt32(strResult), "FK_portal_tags_portal_articles", 0, 0);
                        }
                        catch { continue; }
                }
                foreach (string rela in related.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
                {
                    if (!string.IsNullOrEmpty(rela))
                        try
                        {
                            BO_portal_articles_related.Insert(Convert.ToInt32(strResult), Convert.ToInt32(BO_portal_articles.Search(rela, 1).Rows[0]["ARTICLE_ID"]));
                        }
                        catch { continue; }
                }
                Clear(this.Controls);
            }
            else
                divMessage.InnerHtml = MessageBox.Error("Lỗi", "Có lỗi trong quá trình xử lý! Đăng tin thất bại!");
        }
        catch (Exception ex)
        {
            divMessage.InnerHtml = MessageBox.Error("Lỗi", "Phát sinh lỗi như sau: " + ex.Message);
        }
    }
}
